package com.itheima.controller.backend;


import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.AliOssUtil;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {


    @Autowired
    private SetmealService setmealService;
    //注入阿里云工具类对象
    @Autowired
    private AliOssUtil aliOssUtil;
    // 注入Redis Template类
    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 添加体验套餐
     * @param checkgroupIds
     * @param setmeal
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestParam String checkgroupIds,
                      @RequestBody Setmeal setmeal){
        log.info("添加套餐，检查项id：{}",checkgroupIds);

        setmealService.add(checkgroupIds,setmeal);

        Result result = new Result();
        result.setFlag(true);
        result.setMessage(MessageConstant.ADD_SETMEAL_SUCCESS);

        return result;

    }

    @PostMapping("/edit")
    public Result edit(@RequestParam String checkGroupIds,
                       @RequestBody Setmeal setmeal){
        log.info("编辑套餐，检查项id：{}",checkGroupIds);
        log.info("编辑套餐，检查项id：{}",checkGroupIds);
        setmealService.edit(checkGroupIds,setmeal);
        Result result = new Result();
        result.setFlag(true);
        result.setMessage(MessageConstant.EDIT_SETMEAL_SUCCESS);
        return result;
    }

    // 根据id查询套餐
    @GetMapping("/findById")
    public Result findById(@RequestParam Integer id){
        log.info("根据id查询套餐：{}",id);
        Result result = new Result();
        result.setFlag(true);
        result.setData(setmealService.findById(id));
        return result;
    }

    // 分页查询
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        log.info("分页查询套餐参数：{}",queryPageBean);
        Result result = new Result();
        result.setFlag(true);
        result.setData(setmealService.findPage(queryPageBean));
        return result;
    }

    //图片上传
    @PostMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
        log.info("图片上传：{}",imgFile);
        // 获取原始文件名
        String originalFilename = imgFile.getOriginalFilename();
        // 截取原始文件名后缀
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        //构建新的文件名
        String newFileName = UUID.randomUUID().toString() + extension;

        // 文件请求路径
        try {
            String filePath = aliOssUtil.upload(imgFile.getBytes(),newFileName);
            log.info("图片名字", newFileName);
            // 将上传成功的图片存储在redis中的一个集合中
            redisTemplate.opsForSet().add("uploaded_images",filePath);
            Result result = new Result();
            result.setFlag(true);
            result.setMessage(MessageConstant.PIC_UPLOAD_SUCCESS);
            result.setData(filePath);

            return result;
        }catch (IOException e){
            log.error(MessageConstant.PIC_UPLOAD_SUCCESS + ":{}" ,e);
        }
        Result result = new Result();
        result.setFlag(true);
        return result;
    }

    /**
     * 获取所有套餐信息
     */
    @PostMapping("/getSetmeal")
    public Result getSetmeal(){
        Result result = new Result();
        result.setFlag(true);
        result.setData(setmealService.getSetmeal());
        return result;
    }


}
